#include "engine.h"

#include "object.h"
#include "table.h"
#include "ball.h"
#include "paddle.h"
#include "score.h"
#include <windows.h>
#include <SDL.h>

static volatile bool running = false;

void process_events()
{
     SDL_Event message;

     while (SDL_PollEvent(&message)) {
          switch (message.type) {
          case SDL_QUIT:
               running = false;
               break;
          }
     }
}


int main(int argc, char* argv[])
{
     console_output("Pong\n");
     initialise_engine(640, 480);

     float play_field[] = { 320.0f, 240.0f };

	float paddle_1_position[] = { -312.f, 0.0f };
	float paddle_2_position[] = {  312.f, 0.0f };
	float score_1_position[] = { -160.0f, 200.0f };
	float score_2_position[] = {  160.0f, 200.0f };

	paddle * player1 = new paddle("paddle.bmp", paddle_1_position, SDLK_a, SDLK_z, play_field);
	paddle * player2 = new paddle("paddle.bmp", paddle_2_position, SDLK_UP, SDLK_DOWN, play_field);
	score * score1 = new score("numbers.bmp", score_1_position);
	score * score2 = new score("numbers.bmp", score_2_position);
	table * court = new table("table.bmp");
	ball * the_ball = new ball("ball.bmp", play_field, player1, score1, player2, score2);

     object * instance[] = {
          court,
		score1,
		score2,
          the_ball,
		player1,
		player2,
     };

     const int number_of_objects = sizeof(instance) / sizeof(object*);
     const float time = 1.0f/60.0f;

     running = true;

     while (running) {

          process_events();

          for (int i = 0; i < number_of_objects; ++i) {

               instance[i]->update(time);
          }

          start_render();

          for (int i = 0; i < number_of_objects; ++i) {

               instance[i]->draw();
          }

          end_render();
     }

     for (int i = 0; i < number_of_objects; ++i) {

          delete instance[i];
     }

     quit(0);

     return 0;
}
